package cn.fl.custpay.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmm.mid.core.framework.dto.DataResultDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.fl.custpay.service.ICmmPaymentRecordApiService;
import cn.fl.system.service.ISysDictionaryService;
import com.alibaba.fastjson.JSON;
import com.hazelcast.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.custpay.condition.CmmRechargeRecordQueryCondition;
import cn.fl.custpay.dto.CmmRechargeRecordDTO;
import cn.fl.custpay.service.ICmmRechargeRecordApiService;
import cn.fl.custpay.service.ICmmRechargeRecordService;
import cn.fl.custpay.transform.CmmRechargeRecordVOTransform;
import cn.fl.custpay.vo.CmmRechargeRecordVO;
import cn.fl.framework.base.context.CurrentThreadContext;

/**
 * @Description: 还款记录 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class CmmRechargeRecordServiceImpl implements ICmmRechargeRecordService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(ICmmRechargeRecordService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	//@Autowired
	//private I自定义ApiService 自定义ApiService;
	@Autowired
	private ICmmRechargeRecordApiService cmmRechargeRecordApiService;

	private ICmmPaymentRecordApiService cmmPaymentRecordApiService;

	/**
	 * 组织架构 API 暴露服务
	 */
	@Autowired
	private ISysOrgApiService sysOrgApiService;

	@Autowired
	private ISysDictionaryService sysDictionaryService;

	@Override
	public CmmRechargeRecordVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		CmmRechargeRecordDTO dto = cmmRechargeRecordApiService.selectById(id);
		return CmmRechargeRecordVOTransform.toVO(dto);
	}

	@Override
	public Long add(CmmRechargeRecordVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.cmmRechargeRecordApiService.add(CmmRechargeRecordVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.cmmRechargeRecordApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(CmmRechargeRecordVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.cmmRechargeRecordApiService.modifyRecharge(CmmRechargeRecordVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page) return new Pager();
		Pager resultpager = this.cmmRechargeRecordApiService.searchCmmRechargeRecordListPage(page);
		List<CmmRechargeRecordVO> list = CmmRechargeRecordVOTransform.toVOList((List<CmmRechargeRecordDTO>)resultpager.getResultList());
		resultpager.setResultList(list);
		return resultpager;
	}

	@Override
	public List<KeyValueVO> searchListByOrgTypCd(String sysCd) throws BaseException {
		List<SysOrgDTO> list = this.sysOrgApiService.searchListByOrgTypCd(sysCd);
		List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
		if(CollectionUtils.isEmpty(list)){
			return kvList;
		}
		for (SysOrgDTO sysOrgDTO : list) {
			KeyValueVO kv = new KeyValueVO();
			kv.setKey(sysOrgDTO.getOrgCd());
			kv.setValue(sysOrgDTO.getOrgNm());
			kvList.add(kv);
		}
		return kvList;
	}

	@Override
	public String searchOrgNm(String currentBuOrgCd, String currentSysCd) throws BaseException {
		String orgnm = "";
		if(StringUtils.isNotBlank(currentBuOrgCd)){
			SysOrgDTO sysOrgDto = 	this.sysOrgApiService.selectSysOrgByOrgCd(currentBuOrgCd,currentSysCd);
			if(sysOrgDto != null){
				orgnm = sysOrgDto.getOrgDispNm();
			}
		}
		return orgnm;
	}

	@Override
	public List<CmmRechargeRecordVO> searchList(CmmRechargeRecordQueryCondition qc) throws BaseException {
		List<CmmRechargeRecordDTO> dtolist = this.cmmRechargeRecordApiService.searchCmmRechargeRecordList(qc);
		return CmmRechargeRecordVOTransform.toVOList(dtolist);
	}

	@Override
	public void autoChargePay() throws BaseException {
		cmmRechargeRecordApiService.autoChargePay();
	}

	@Override
	public void autoFundDecompose() throws BaseException {
		this.cmmRechargeRecordApiService.autoFundDecompose();
	}

	@Override
	public CmmRechargeRecordVO unifyDeductedQuery(Long orderId) throws BaseException {
		return CmmRechargeRecordVOTransform.toVO(this.cmmRechargeRecordApiService.unifyDeductedQuery(orderId));
	}

	@Override
	public Map<String,String> getDictionryMap(final String cd_type){
		List<String> codeGrpCdList = new ArrayList<>();
		codeGrpCdList.add(cd_type);
		Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
		List<KeyValueVO> statusList = dicMap.get(cd_type);
		Map<String, String> statusMap = null;
		if (CollectionUtils.isNotEmpty(statusList)) {
			statusMap = new HashMap<>(statusList.size());
			for (KeyValueVO dto : statusList) {
				statusMap.put(dto.getKey(), dto.getValue());
			}
		}
		return statusMap;
	}

	@Override
	public void fundDecompose(Long orderId) throws BaseException {
		this.cmmRechargeRecordApiService.fundDecompose(orderId);
	}

	@Override
	public void paautodecoposeFund(CmmRechargeRecordVO vo) throws BaseException {
		this.cmmRechargeRecordApiService.paautodecoposeFund(CmmRechargeRecordVOTransform.toDTO(vo));
	}

	@Override
	public String queryChl(String orderid, String s) {
		return cmmRechargeRecordApiService.queryChl(orderid, "1");
	}
}
